package chapter1.bagqueuestack.exercise;

import chapter1.bagqueuestack.example.Stack;
import edu.princeton.cs.algs4.StdIn;

/**
 * 从标准输入中得到一个后序表达式，求值并打印结果
 * 2 3 4 + 5 6 * * +
 *
 * @author quanlinlin on 2018/12/2 18:08.
 * @version 1.0
 */
public class EvaluatePostfix {

    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        while (!StdIn.isEmpty()) {
            String s = StdIn.readString();
            if (s.equals("+")) {
                stack.push(stack.pop() + stack.pop());
            } else if (s.equals("*")) {
                stack.push(stack.pop() * stack.pop());
            } else {
                stack.push(Integer.valueOf(s));
            }
        }
        System.out.println(stack.pop());
    }
}
